package com.violet.gateway.config;

import com.alibaba.cloud.nacos.NacosConfigManager;
import com.violet.gateway.route.NacosRouteDefinitionRepository;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * 类说明:动态路由配置
 * 配置文件配置路由，地址、端口 都是写死的，就没办法做负载均衡了。所以这一节，就配置动态路由。
 * @author wqf
 * @date 2022/10/26 17:58
 */
@Configuration
@ConditionalOnProperty(prefix = "violet.gateway.dynamicRoute", name = "enabled", havingValue = "true")
public class DynamicRouteConfig {
    @Resource
    private ApplicationEventPublisher publisher;

    /**
     * Nacos实现方式
     */
    @Configuration
    @ConditionalOnProperty(prefix = "violet.gateway.dynamicRoute", name = "dataType", havingValue = "nacos", matchIfMissing = true)
    public class NacosDynRoute {
        @Resource
        private NacosConfigManager nacosConfigManager;

        @Bean
        public NacosRouteDefinitionRepository nacosRouteDefinitionRepository() {
            return new NacosRouteDefinitionRepository(publisher, nacosConfigManager);
        }
    }
}
